package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.Contact;
import com.koalac.ironman.net.pojo.response.ContactDetail;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.LoadingDialog;
import com.squareup.otto.Subscribe;

public class ContactActivity extends ModuleActivity implements OnClickListener{

    @ViewInject(id = R.id.parent_layout)
    View parentLayout;
    @ViewInject(id = R.id.foot_travel)
    TextView mFootTravel1View;
    @ViewInject(id = R.id.phone1,click = "onClick")
    TextView mPhone1View;
    @ViewInject(id = R.id.qq1,click = "onClick")
    TextView mQQ1View;
    @ViewInject(id = R.id.weixin1,click = "onClick")
    TextView mWeixin1View;

    @ViewInject(id = R.id.foot_travel2)
    TextView mFootTravel2View;
    @ViewInject(id = R.id.phone2,click = "onClick")
    TextView mPhone2View;
    @ViewInject(id = R.id.qq2,click = "onClick")
    TextView mQQ2View;
    @ViewInject(id = R.id.weixin2,click = "onClick")
    TextView mWeixin2View;

    ContactDetail mContactDetail;
    Contact mContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("联系我们");
	setBackButtonVisibility(true);
	parentLayout.setVisibility(View.INVISIBLE);
	setContentLayoutBackgroudColor(getResources().getColor(R.color.module_color1));
	init();
    }

    private void init() {
	mContact = new Contact();
	final LoadingDialog loadingDialog = showLoadingDialog("获取联系我们详情...");
	MyRequest.request(ContactActivity.this, mContact,
		new ResponseCallback() {

		    @Override
		    public void process(boolean result, Context context,
			    Response data, String msg) {
			canceledLoadingDialog(loadingDialog);
			if (result) {
			    parentLayout.setVisibility(View.VISIBLE);
			    mContactDetail = data
				    .getObject(ContactDetail.class);
			    mFootTravel1View.setText(mContactDetail.getName());
			    mFootTravel2View.setText(mContactDetail.getName2());

			    mPhone1View.setText(mContactDetail.getPhone());
			    mPhone2View.setText(mContactDetail.getPhone2());

			    mQQ1View.setText(mContactDetail.getQq());
			    mQQ2View.setText(mContactDetail.getQq2());

			    mWeixin1View.setText(mContactDetail.getWeixin());
			    mWeixin2View.setText(mContactDetail.getWeixin2());
			    setNetworkVisibility(false);
			}else {
			    setNetworkVisibility(true);
			}
		    }
		});
    }
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	setNetworkVisibility(false);
	init();
    }

    private void callPhone(String phoneNum) {
	if (!StringUtils.isEmpty(phoneNum)) {
	    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
	          startActivity(intent);
	}else {
	    toast("电话号码为空!");
	}
    }

    private void callQQ(String qq) {
	if (!StringUtils.isEmpty(qq)) {
	    String url="mqqwpa://im/chat?chat_type=wpa&uin=" + qq;
	    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
	}else {
	    toast("qq号码为空!");
	}
    }

    private void callWeixin(String weixin) {

    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_contact);
    }

    @Override
    public void onClick(View v) {
	int viewID = v.getId();
	switch (viewID) {
	case R.id.phone1:
	    callPhone(mPhone1View.getText().toString());
	    break;
	case R.id.qq1:
	    callQQ(mQQ1View.getText().toString());
	    break;
	case R.id.weixin1:
	    
	    break;
	case R.id.phone2:
	    callPhone(mPhone2View.getText().toString());
	    break;
	case R.id.qq2:
	    callQQ(mQQ2View.getText().toString());
	    break;
	case R.id.weixin2:
	    
	    break;

	}
    }

}
